package cd.safe.school.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cd.safe.school.R;
import cd.safe.school.adapter.CompostionAdapter;
import cd.safe.school.adapter.EnglishEditAdapter;
import cd.safe.school.bean.SharePager;
import cd.safe.school.data.AppData;
import cd.safe.school.util.DateUtil;
import cd.safe.school.util.DownloadUpload;
import cd.safe.school.util.Parse;
import cd.safe.school.util.XmlUtil;
import cd.safe.school.util.ZipUtil;

public class SuperCompositionActivity extends Activity {
	private ListView compsition;
	private List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>();
	private List<SharePager> lst = new ArrayList<SharePager>();
	private Handler handler = new Handler();
	public static Handler handler2;
	private ProgressDialog pd;
	private Timer timer = new Timer();
	private String music_path;
	private AppData ad = new AppData(this);
	private SQLiteDatabase db;
	private String coursepath;
	private String img_path;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.compsition);
		compsition = (ListView) findViewById(R.id.composition);
		HomeActivity.uag=2;
		db = ad.getWritableDatabase();
		pd = ProgressDialog.show(this, "", "数据加载中...");
		pd.setCancelable(true);

		new Thread(new Runnable() {
			@Override
			public void run() {

				lst = Parse.parsepage(2, 1, 7);

				final CompostionAdapter adapter = new CompostionAdapter(
						SuperCompositionActivity.this, lst);
				handler.post(new Runnable() {

					@Override
					public void run() {
						pd.dismiss();
						if (lst.size() > 0) {
							compsition.setAdapter(adapter);
						} else {
							Toast.makeText(SuperCompositionActivity.this,
									"获取数据失败...", Toast.LENGTH_SHORT).show();
						}
					}
				});

			}
		}).start();

		compsition.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 下载 解压

				Cursor c = db.query("Courseware", null, "courseID=?",
						new String[] { String
								.valueOf(lst.get(position).getId()) }, null,
						null, null);
				if (c.moveToNext()) {
			
						coursepath = c.getString(c.getColumnIndex("path"));
						if (coursepath != null && !"".equals(coursepath)) {
							File file = new File(coursepath);
							if (!file.exists()) {
								coursepath = Environment
										.getExternalStorageDirectory()
										+ "/safeschool/composition/"
										+ lst.get(position).getId()
										+ "/kejian.zip";
								ContentValues values = new ContentValues();
								values.put("path", coursepath);

								db.update(
										"Courseware",
										values,
										"courseID=?",
										new String[] { String.valueOf(lst.get(
												position).getId()) });

								DownloadUpload.downloadefile(lst.get(position)
										.getUrl(), coursepath);
							}
						} else {
							coursepath = Environment
									.getExternalStorageDirectory()
									+ "/safeschool/composition/"
									+ lst.get(position).getId() + "/kejian.zip";
							ContentValues values = new ContentValues();
							values.put("path", coursepath);

							db.update(
									"Courseware",
									values,
									"courseID=?",
									new String[] { String.valueOf(lst.get(
											position).getId()) });
							DownloadUpload.downloadefile(lst.get(position)
									.getUrl(), coursepath);

						}
					

				} else {
					coursepath = Environment.getExternalStorageDirectory()
							+ "/safeschool/composition/"
							+ lst.get(position).getId() + "/kejian.zip";

					ContentValues values = new ContentValues();
					values.put("path", coursepath);
					values.put("courseID", lst.get(position).getId());
					values.put("createTime", DateUtil.getCurrentDate());
					values.put("type", lst.get(position).getType());
					db.insert("Courseware", "courseID", values);

					DownloadUpload.downloadefile(lst.get(position).getUrl(),
							coursepath);
				}

				try {

					ZipUtil.upZipSelectedFile(
							new File(coursepath),
							Environment.getExternalStorageDirectory()
									+ "/safeschool/composition/"
									+ lst.get(position).getId());

					XmlUtil.parsexml(Environment.getExternalStorageDirectory()
							+ "/safeschool/composition/"
							+ lst.get(position).getId() + "/GZ.xml");
					music_path = Environment.getExternalStorageDirectory()
							+ "/safeschool/composition/"
							+ lst.get(position).getId() + "/"
							+ XmlUtil.musicname;
					img_path = Environment.getExternalStorageDirectory()
							+ "/safeschool/composition/"
							+ lst.get(position).getId();

				} catch (Exception e) {
					e.printStackTrace();
					handler2.sendEmptyMessage(1001);
				}

				// ------------------------

				Message msg = new Message();
				msg.what = 3;
				Bundle bun = msg.getData();
				bun.putString("music_path", music_path);
				bun.putString("img_path", img_path);
				bun.putString("tag", "composition");

				handler2.sendMessage(msg);

			}
		});

	}

}
